package com.custom;

import com.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 本案例演示定制队列，交换机，绑定的配置（模仿Rabbitmq官网的topic手册的例子）
 *
 * 先声明交换机以及交换机与队列的绑定情况（依据路由key的模式），支持*与#2个特殊符号
 *
 * 发送消息时依据routing key，把消息从交换机分发到符合绑定的所有队列中去
 */
@Configuration
@EnableRabbit
@ComponentScan("com.custom")
public class CustomBindingMqConfiguration {

    public static final String EXCHANGE_TOPIC_NAME = "topicA";
    @Bean
    public CachingConnectionFactory connectionFactory() {
        return new CachingConnectionFactory("localhost");
    }

    @Bean
    public RabbitAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public Queue myQueueA() {
        return new Queue("q1");
    }



    //推荐用这种builder的写法来创建对象（QueueBuilder，BindingBuilder，MessageBuilder）
    @Bean
    public Queue myQueueB() {
        return QueueBuilder.durable("q2").build();
    }

    /*   @Bean
   public Queue myQueueC() {

       Queue queue = new Queue("girls");
       queue.setShouldDeclare(true);
       queue.addArgument("arg1","arg1 value");
       return queue;
   }*/

    @Bean
    public TopicExchange topicExchange(){
        TopicExchange topicExchange = new TopicExchange(EXCHANGE_TOPIC_NAME);
        return topicExchange;
    }

    /**
     *  *(星号）表示一个单词，#（井号）表示0到多个单词
     * @return
     */
    @Bean
    public Binding bindingA(){
        Binding binding = new Binding("q1", Binding.DestinationType.QUEUE,EXCHANGE_TOPIC_NAME,"*.orange.*",null);
        return binding;
    }

    @Bean
    public Binding bindingB(){
        return BindingBuilder.bind(myQueueB()).to(topicExchange()).with("*.*.rabbit");

    }

    @Bean
    public Binding bindingC(){
        return BindingBuilder.bind(myQueueB()).to(topicExchange()).with("lazy.*.#");

    }


    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        //设置发送消息的消息转换器
        template.setMessageConverter(messageConverter());
        return template;
    }


    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        //设置接收消息的消息转换器
        factory.setMessageConverter(messageConverter());

        return factory;
    }
}
